package com.online.demo.service.impl;

import com.online.demo.service.IFreemarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

public class FreemarkerServiceImpl implements IFreemarkerService {

    @Resource
    private Configuration configuration;

    // 模板名称：xxx.ftl  -- 在templates目录下，需要是ftl结尾的
    // 0 表示生成成功
    @Override
    public int createContentStaticPage(String templateName, Map<String, Object> params) throws IOException, TemplateException {

        String contentStaticPageUrl = "/srv/online/content/";

        Template template = configuration.getTemplate(templateName);

        FileWriter fw = new FileWriter(contentStaticPageUrl + "页面名称.html");
        template.process(params,fw);

        return 0;
    }
}
